<?php

/**
 * This is the model class for table "{{trade}}".
 *
 * The followings are the available columns in table '{{trade}}':
 * @property integer $ztr_id
 * @property integer $ztr_type
 * @property integer $zca_id
 * @property integer $zar_id
 * @property integer $zm_id
 * @property string $ztr_title
 * @property string $ztr_content
 * @property double $ztr_price
 * @property string $ztr_unit
 * @property integer $ztr_nums
 * @property string $ztr_pic
 * @property integer $ztr_status
 * @property integer $ztr_draft
 * @property integer $ztr_exp_time
 * @property integer $ztr_exp_days
 * @property integer $ztr_commend
 * @property integer $ztr_uregent
 * @property integer $ztr_lockend
 * @property integer $ztr_point
 * @property string $zmg_id
 * @property string $ztr_ip
 * @property integer $ztr_hist
 * @property string $ztr_tag_ids
 * @property integer $ztr_display
 * @property integer $ztr_del
 * @property integer $ztr_send
 * @property integer $ztr_listorder
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Trade extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Trade the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{trade}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ztr_title, ztr_nums, ztr_unit, ztr_price, ztr_exp_time, ztr_content, zar_id, ztr_linkman, ztr_mob', 'required'),
			array('ztr_type, zca_id, zar_id, zm_id, ztr_nums, ztr_status, ztr_draft, ztr_exp_days, ztr_commend, ztr_uregent, ztr_lockend, ztr_point, ztr_hist, ztr_display, ztr_del, ztr_send, ztr_listorder, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('ztr_price', 'numerical'),
			array('ztr_mob', 'match', 'pattern'=>'%^[0-9\-]{7,16}$%', 'message'=>'请输入正确的联系电话'),
			array('ztr_title, ztr_exp_time', 'length', 'max'=>100),
			array('ztr_unit,ztr_linkman', 'length', 'max'=>10),
			array('ztr_pic, ztr_tag_ids', 'length', 'max'=>255),
			array('zmg_id', 'length', 'max'=>50),
			array('ztr_ip', 'length', 'max'=>15),
			array('ztr_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ztr_id, ztr_type, zca_id, zar_id, zm_id, ztr_title, ztr_content, ztr_price, ztr_unit, ztr_nums, ztr_pic, ztr_status, ztr_draft, ztr_exp_time, ztr_exp_days, ztr_commend, ztr_uregent, ztr_lockend, ztr_point, zmg_id, ztr_ip, ztr_hist, ztr_tag_ids, ztr_display, ztr_del, ztr_send, ztr_listorder, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'memeber'=>array(self::BELONGS_TO, 'Member', 'zm_id'),//会员
				'category'=>array(self::BELONGS_TO, 'Category', 'zca_id'),//行业分类
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ztr_id' => 'Ztr',
			'ztr_type' => 'Ztr Type',
			'zca_id' => 'Zca',
			'zar_id' => 'Zar',
			'zm_id' => 'Zm',
			'ztr_linkman' => '联系人',
			'ztr_mob' => '联系电话',
			'ztr_title' => '求购标题',
			'ztr_content' => '内容描述',
			'ztr_price' => '价格要求',
			'ztr_unit' => '单位',
			'ztr_nums' => '数量',
			'ztr_pic' => 'Ztr Pic',
			'ztr_status' => 'Ztr Status',
			'ztr_draft' => 'Ztr Draft',
			'ztr_exp_time' => '有效期至',
			'ztr_exp_days' => 'Ztr Exp Days',
			'ztr_commend' => 'Ztr Commend',
			'ztr_uregent' => 'Ztr Uregent',
			'ztr_lockend' => 'Ztr Lockend',
			'ztr_point' => 'Ztr Point',
			'zmg_id' => 'Zmg',
			'ztr_ip' => 'Ztr Ip',
			'ztr_hist' => 'Ztr Hist',
			'ztr_tag_ids' => 'Ztr Tag Ids',
			'ztr_display' => 'Ztr Display',
			'ztr_del' => 'Ztr Del',
			'ztr_send' => 'Ztr Send',
			'ztr_listorder' => 'Ztr Listorder',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ztr_id',$this->ztr_id);
		$criteria->compare('ztr_type',$this->ztr_type);
		$criteria->compare('zar_id',$this->zar_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('ztr_linkman',$this->ztr_linkman);
		$criteria->compare('ztr_mob',$this->ztr_mob);
		$criteria->compare('ztr_title',$this->ztr_title,true);
		$criteria->compare('ztr_content',$this->ztr_content,true);
		$criteria->compare('ztr_price',$this->ztr_price);
		$criteria->compare('ztr_unit',$this->ztr_unit,true);
		$criteria->compare('ztr_nums',$this->ztr_nums);
		$criteria->compare('ztr_pic',$this->ztr_pic,true);
		$criteria->compare('ztr_status',$this->ztr_status);
		$criteria->compare('ztr_draft',$this->ztr_draft);
		$criteria->compare('ztr_exp_time',$this->ztr_exp_time);
		$criteria->compare('ztr_exp_days',$this->ztr_exp_days);
		$criteria->compare('ztr_commend',$this->ztr_commend);
		$criteria->compare('ztr_uregent',$this->ztr_uregent);
		$criteria->compare('ztr_lockend',$this->ztr_lockend);
		$criteria->compare('ztr_point',$this->ztr_point);
		$criteria->compare('zmg_id',$this->zmg_id,true);
		$criteria->compare('ztr_ip',$this->ztr_ip,true);
		$criteria->compare('ztr_hist',$this->ztr_hist);
		$criteria->compare('ztr_tag_ids',$this->ztr_tag_ids,true);
		$criteria->compare('ztr_display',$this->ztr_display);
		$criteria->compare('ztr_del',$this->ztr_del);
		$criteria->compare('ztr_send',$this->ztr_send);
		$criteria->compare('ztr_listorder',$this->ztr_listorder);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		
		//行业分类
		if(!empty($this->zca_id)){
			$criteria->join = ' LEFT JOIN {{category}} zca ON zca.zca_id=t.zca_id ';
			$criteria->addCondition(" (zca.zca_id={$this->zca_id} OR zca.zca_parentid={$this->zca_id} OR zca.zca_topid={$this->zca_id})");
		}
		
		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 是否推荐
	 */
	public function getZtrCommend($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是'
			);
		}else{
			$data = $this->getZtrCommend();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}
	
	/**
	 * 是否头条
	 */
	public function getCtrUregent($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是'
			);
		}else{
			$data = $this->getCtrUregent();
			if(array_key_exists($type,$data))
				return $data[$type];
		}
	}
	
	/**
	 * 保存前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->ztr_ip = Yii::app()->request->userHostAddress;
			$this->inputtime = time();

			if(Yii::app()->user->getstate('manage')=="admin"){
				//后台管理员
				$this->zm_id = 0 ;
				$this->ztr_status = 2;//审核状态通过
			}elseif(Yii::app()->user->getstate('usertype')=="sub_member"){
				//商家子账户
				$this->zm_id = Company::model()->getCmpUserId();
				$this->ztr_status = F::setting("member_trade_checked");//审核状态系统配置
			}else{
				//正常用户
				$this->zm_id = Yii::app()->user->getId();
				$this->ztr_status = F::setting("member_trade_checked");//审核状态系统配置
			}
		}else{
			$this->updatetime = time();
		}
		return true;
	}
}